# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=visualizationlibrary
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.1.0.r4.gd2a0e32
pkgrel=1
pkgdesc="C++ middleware for high-performance 2D and 3D graphics applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://visualizationlibrary.org/"
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw"
             'git')
_commit='d2a0e321288152008957e29a0bc270ad192f75be'
source=("${_realname}"::"git+https://github.com/MicBosi/VisualizationLibrary.git#commit=${_commit}"
        0001-vl-remove-std-mem-fun-ref.patch
        0002-vl-fix-wx-macros.patch
        0003-vl-fix-clang-errors.patch)
sha256sums=('SKIP'
            '6f194c1103c94575be8269c3ba5affdf2f73a92a46a596244d1af2141d77bd90'
            '24d1e0b1937b783e16e8b0c9d8caf576941c40312464fab5fb681fd898814207'
            'beb0127a83e5c9e7833a3d0851ae1316fc1e72c719b491f357574dca1ee72938')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    git apply "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd "${_realname}"
  git describe --long --tags --abbrev=7 "${_commit}" | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${_realname}"
  find src -type f -exec dos2unix -q {} \;

  # for clang, due to cmath includes its own <version> headers
  rm "src/external/libtiff/version"

  # 0001: https://github.com/GPSnoopy/TriStripper/commit/79fd402a43a9e92aafc155f88ff53a0b044f1e81.patch
  # 0002: https://github.com/MicBosi/VisualizationLibrary/pull/211
  _apply_patch_with_msg \
    0001-vl-remove-std-mem-fun-ref.patch \
    0002-vl-fix-wx-macros.patch \
    0003-vl-fix-clang-errors.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DVL_GUI_WIN32_EXAMPLES=OFF \
      -DVL_GUI_WIN32_SUPPORT=ON \
      -DVL_GUI_WXWIDGETS_EXAMPLES=OFF \
      -DVL_GUI_WXWIDGETS_SUPPORT=ON \
      -DVL_UNIX_INSTALL_MODE=ON \
      -B "build-${MSYSTEM}" \
      -S "${_realname}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"
}
